<?php
// Controller/Default.php

class Controller_study extends FLEA_Controller_Action
{
    function actionindex() {
        $category = $_GET['category'];
        if(empty($category)){
            $category = 'tofoword';
        }
        $table_pics =& FLEA::getSingleton('Table_'.$category);
        $word = $table_pics->find(1);
        $smarty =& $this->_getView();
        $smarty->assign("category",$category);
        $smarty->assign("word",$word[Word]);
        $smarty->assign("id",$word[WordID]);
        $smarty->display('study.html');
    }
    function actionimage() {
        $word = $_GET['word']?trim($_GET['word']):"hello";
        $no = $_GET['no']?trim($_GET['no']):1;
        FLEA::loadFile('FLEA_FLEA_Ajax_JSON.php',true);
        $json = new Services_JSON();
        $table_pics =& FLEA::getSingleton('Table_tofowordpic');
        // $pics = $table_pics->findBySql('SELECT a.id as pic_id,c.id as voice_id,b.pic_text,d.En_Sen,d.Chi_Sen FROM `tofoword_pic` a inner join (tofoword_text b,tofoword_voice c,word_examplesentence d) on (a.No=b.No and a.Word=b.Word and a.No=c.No and a.Word=c.Word and a.No=d.No and a.Word=d.Word) where a.Word ="' . $word . '" order by a.No limit 1');
        $pics = $table_pics->findBySql('SELECT a.id as pic_id FROM `tofoword_pic` a where a.Word ="' . $word . '" and a.No =' . (int)$no);
        $pics = $pics[0];
        print $json->encode($pics);
    }
    function actiongetword(){
        $op = trim($_GET['op']);
        $no = trim($_GET['no']);
        $word = trim($_GET['word']);
        $id = trim($_GET['id']);
        $category = trim($_GET['category']);
        if(empty($category)){
            $category = 'tofoword';
        }
        FLEA::loadFile('FLEA_FLEA_Ajax_JSON.php',true);
        $json = new Services_JSON();
        $table_words =& FLEA::getSingleton('Table_'.$category);
        if($op == "prev"){
            if((int)$no == 1){
                // $words = $table_words->findBySql("select WordID as id,Word as word from tofoword where tofoword.WordID<".$id . " order by WordID desc limit 1");
                $words = $table_words->find("WordID<".$id,"WordID","WordID,Word");
                // $words[0]['no'] = "8";
                $words[0]['no'] = "8";
                print $json->encode($words[0]);
            }
            else{
                print $json->encode(array('id'=>$id,'word'=>$word,'no'=>(string)--$no));
            }
        }
        elseif($op == "next"){
            if((int)$no == 8){
                // $words = $table_words->findBySql("select WordID as id,Word as word from tofoword where tofoword.WordID>".$id . " limit 1");
                $words = $table_words->find("WordID>".$id,"WordID","WordID,Word");
                $words['no'] = "1";
                $words['word'] = $words['Word'];
                $words['id'] = $words['WordID'];
                unset($words['Word']);
                unset($words['WordID']);
                print $json->encode($words);
            }
            else{
                print $json->encode(array('id'=>$id,'word'=>$word,'no'=>(string)++$no));
            }
        }
    }
    function actiongetwordbegin(){
        $word = trim($_GET['word']);
        FLEA::loadFile('FLEA_FLEA_Ajax_JSON.php',true);
        $json = new Services_JSON();
        $table_words =& FLEA::getSingleton('Table_tofoword');
        $words = $table_words->findBySql("select WordID as id,Word as word from tofoword where tofoword.Word=\"".$word . "\"");
        $words[0]['no'] = "1";
        print $json->encode($words[0]);
    }

    function actionmean() {
        $word = $_GET['word']?trim($_GET['word']):"hello";
        FLEA::loadFile('FLEA_FLEA_Ajax_JSON.php',true);
        $json = new Services_JSON();
        $table_yinbiao = & FLEA::getSingleton('Table_word2yinbiao');
        $yinbiao = $table_yinbiao->find(array('Word'=>$word),null,'Word,YinBiao,HanYuJieshe');
        $yinbiao[YinBiao] = iconv("GB2312","UTF-8",$yinbiao[YinBiao]);
        $yinbiao[HanYuJieshe] = iconv("GB2312","UTF-8",$yinbiao[HanYuJieshe]);
        print $json->encode($yinbiao);
    }
    function actionexplain() {
        $word = $_GET['word']?trim($_GET['word']):"hello";
        $no = $_GET['no']?trim($_GET['no']):"1";
        FLEA::loadFile('FLEA_FLEA_Ajax_JSON.php',true);
        $json = new Services_JSON();
        $table_text = & FLEA::getSingleton('Table_tofowordtext');
        $text = $table_text->find(array('Word'=>$word,'No'=>(int)$no));
        print $json->encode($text);
    }
    function actionexplainbyzh() {
        $word = $_GET['word']?trim($_GET['word']):"hello";
        $no = $_GET['no']?trim($_GET['no']):"1";
        FLEA::loadFile('FLEA_FLEA_Ajax_JSON.php',true);
        $json = new Services_JSON();
        $table_text = & FLEA::getSingleton('Table_tofowordtext');
        $text = $table_text->find(array('Word'=>$word,'No'=>(int)$no),null,'pic_text_zh');
        print $json->encode($text);
    }
    function actionsentence() {
        $word = $_GET['word']?trim($_GET['word']):"hello";
        $no = $_GET['no']?trim($_GET['no']):"1";
        FLEA::loadFile('FLEA_FLEA_Ajax_JSON.php',true);
        $json = new Services_JSON();
        $table_text = & FLEA::getSingleton('Table_wordsentence');
        $text = $table_text->find(array('Word'=>$word,'No'=>(int)$no));
        $table_voice = & FLEA::getSingleton('Table_tofowordvoice');
        $voice = $table_voice->find(array('Word'=>$word,'No'=>(int)$no),null,'id');
        $text['Chi_Sen'] = iconv("GB18030","UTF-8",$text['Chi_Sen']);
        $text['id_voice'] = $voice['id'];
        print $json->encode($text);
    }
    function actionshow() {
        $table_words = & FLEA::getSingleton('Table_tofowordpic');
        $smarty =& $this->_getView();
        $words = $table_words->find((int)$_GET['id']);
        $image =  imagecreatefromstring($words[PicBuf]);
        Header("Content-type:image/png"); 
        imagepng($image);
    }
    function actionshowbysize() {
        $table_words = & FLEA::getSingleton('Table_tofowordpic');
        $smarty =& $this->_getView();
        $words = $table_words->find((int)$_GET['id']);
        $image = imagecreatefromstring($words[PicBuf]);
        FLEA::loadFile('APP_Helper_MyImage');
        $out = new resizeimage($image,400,300,0);
        $image = $out->resize();
        Header("Content-type:image/png"); 
        imagepng($image);
    }
    function actionaudio(){
        $table_voice = & FLEA::getSingleton('Table_tofowordvoice');
        $voice = $table_voice->find((int)$_GET['id']);
        Header("Content-type:audio/wav"); 
        print $voice[VoiceData];
    }
}
